import { requestApi } from '../../api';
import { dateFormat } from '../../utils/dateFormat'
import { logUpload } from '../../utils/logUpload'
const app = getApp();

Component({
    properties: {
        filterInfo: {
            type: Object,
            value: null,
            observer(n) {
                this.dataHanlder(n);
            }
        },
        paramsInfo: {
            type: Object,
            value: {},
            observer(n) {
                if (!n.goods_prices) {
                    this.setData({
                        lowPrice: '',
                        highPrice: '',
                        bids: ''
                    });
                }
                n['goods_types'] = n.goods_type_id ? n.goods_type_id : '';
                delete n['goods_type_id'];
                this.setData({
                    params: n,
                });
            }
        },
        newComer: {
            type: Boolean,
            value: false
        },
        pageTop: {
            type: Number,
            value: 0
        }
    },
    data: {
        filterList: null,
        filterTxt: {
            genders: '款式',
            price_levels: '价格',
            brands: '品牌',
            goods_channel_source: '类型'
        },
        lowPrice: '',
        highPrice: '',
        params: {},
        total: 0,
        bids: ''
    },
    pageLifetimes: {
        show() {
            const bids = wx.getStorageSync('bids');
            const { params, paramsInfo } = this.data;

            params['seller_uid'] = paramsInfo.seller_uid ? paramsInfo.seller_uid : '';
            params['keywords'] = paramsInfo.keywords ? paramsInfo.keywords : '';
            params['goods_types'] = paramsInfo.goods_type_id ? paramsInfo.goods_type_id : '';
            params['genders'] = paramsInfo.gender_id ? paramsInfo.gender_id : '';
            params['page_index_id'] = paramsInfo.page_index_id ? paramsInfo.page_index_id : '';
            
            params['brands'] = bids;
            this.setData({
                bids,
                params
            }, ()=> {
                wx.removeStorageSync('bids');
            });
            this.getTotalNum();
        }
    },
    methods: {
        dataHanlder(d) {
            let data = [], n = d.total;

            delete d['total'];
            for(let k in d) {
                if (d[k].length) {
                    data = [].concat(data, this.changeFormat(k, d[k]));
                }
            }

            this.setData({
                total: n,
                filterList: data
            })
        },
        changeFormat(type, list) {
            const { filterTxt } = this.data;
            let d = [], title = filterTxt[type], showMoreStatus = false, isShowMore = list.length > 6 ? true : false, arr = [];
            switch (type) {
                case 'genders':
                    list.forEach(item => {
                        d.push({
                            id: String(item.gender_id),
                            name: item.name_c,
                            name_e: item.name_e
                        })
                    });

                    arr.push({ title, type, isShowMore, showMoreStatus, list: d });
                break;
                case 'price_levels':
                    list.forEach(item => {
                        d.push({
                            id: item.priceid,
                            name: item.name_c,
                            name_e: item.name_e,
                            price: item.price
                        })
                    });
                    isShowMore = list.length > 0 ? true : false;
                    arr.push({ title, type, isShowMore, showMoreStatus, list: d });
                break;
                case 'brands':
                    list.splice(0, 9).forEach((item, index) => {
                        d.push({
                            id: item.bid,
                            name: item.brandname_e,
                            name_e: item.brandname_c
                        })
                    });
                    isShowMore = true;
                    arr.push({ title, type, isShowMore, showMoreStatus, list: d });
                break;
                case 'goods_types':
                    list.forEach(item => {
                        title = item.name;
                        d = item.cate_list;
                        isShowMore = item.cate_list.length > 6 ? true : false;

                        arr.push({ title, type, isShowMore, showMoreStatus, list: d });
                    })
                break;
                case 'goods_channel_source':
                    d = list;
                    arr.push({ title, type, isShowMore, showMoreStatus, list: d });
                break;
            }
            
            return arr;
        },
        showMore(e) {
            const { index, type } = e.currentTarget.dataset;
            const { filterList, bids } = this.data;
            const str = `filterList[${index}].showMoreStatus`;

            if (type != 'brands') {
                this.setData({
                    [str]: !filterList[index].showMoreStatus
                })
            } else {
                wx.navigateTo({
                    url: '/pages/search/searchBrands/searchBrands?multiple=1&bids=' + bids,
                })
            }
        },
        chooseTags(e) {
            const { params, filterInfo } = this.data;
            const { type } = e.currentTarget.dataset;
            const { id } = e.currentTarget;
            let ids = [], low = '', high = '';

            switch (type) {
                case 'genders':
                case 'goods_channel_source':
                    params[type] = params[type] != id ? id : '';
                break;
                case 'price_levels':
                    const info = filterInfo[type].find(item => item.priceid == id);
                    if (info) {
                        low = info.price.split(':')[0] / 100;
                        high = info.price.split(':')[1] / 100;

                        this.setData({
                            lowPrice: low,
                            highPrice: high
                        })
                    }
                    params[type] = params[type] != id ? id : '';
                break;
                case 'brands':
                case 'goods_types':
                    ids = params[type] ? params[type].split(',') : [];
                    const index = ids.findIndex(item => item == id)
                    if (index >= 0) {
                        ids.splice(index, 1);
                    } else {
                        ids.push(id)
                    }

                    params[type] = ids.sort().join(',');

                    if (type == 'brands') {
                        this.setData({
                            bids: ids.join(',')
                        });
                        wx.setStorageSync('bids', ids.join(','));
                    }
                break;
            }

            this.setData({
                params
            });
            this.getTotalNum();
        },
        getTotalNum() {
            const info = this.paramsFormat();
            const type = !info.page_index_id ? 1 : 2; // 金刚区type值是2

            app.request({
                url: requestApi.filterTotalNum,
                data: {
                    params_info: JSON.stringify(info),
                    type,
                    is_new_ver: true
                }
            }, (d) => {
                if (d.original.ret_code == 200) {
                    this.setData({
                        total: d.original.data.total
                    })
                }
            }, (error) => {
                wx.showModal({
                    title: '提示',
                    showCancel: false,
                    content: error,
                })
            })
        },
        paramsFormat() {
            const { params } = this.data;
            const info = {};

            info['keywords'] = params.keyword ? params.keyword : '';

            for(let k in params) {
                switch(k) {
                    case 'genders':
                        info['gender_id'] = params[k];
                        break;
                    case 'price_levels':
                        info['price_id'] = params[k];
                        break;
                    case 'brands':
                        info['brand_id'] = params[k];
                        break;
                    case 'goods_types':
                        info['goods_type_id'] = params[k];
                        break;
                    default:
                        info[k] = params[k];
                }
            }

            return info;
        },
        focusHandler() {
            const { params } = this.data;

            delete params['price_levels'];

            this.setData({
                params
            })
        },
        iptHanler(e) {
            const { id } = e.currentTarget;
            const { value } = e.detail;
            const str = `${id}Price`;

            this.setData({
                [str]: value
            })
        },
        blurHandler() {
            const { params, lowPrice, highPrice } = this.data;
            const arr = [lowPrice * 100, highPrice * 100 || ''];
            delete params['price_levels'];

            params['goods_prices'] = arr.join(':');

            this.setData({
                params
            });
            this.getTotalNum();
        },
        resetHandler() {
            const { params, newComer } = this.data;

            // 是否是新人专区页的筛选
            if (newComer) {
                this.setData({
                    params: {
                        gender_id: params.gender_id,
                        goods_channel_source: 1,
                        is_new_user_buy: 1,
                        keywords: params.keywords,
                        search_goods_filter: 0
                    },
                    lowPrice: '',
                    highPrice: '',
                    bids: ''
                });
            } else {
                this.setData({
                    params: {
                        keywords: params.keywords,
                        seller_uid: params.seller_uid,
                        page_index_id: params.page_index_id
                    },
                    lowPrice: '',
                    highPrice: '',
                    bids: ''
                });
            }

            this.getTotalNum()
        },
        goFilterPage() {
            const info = this.paramsFormat(); 
            this.triggerEvent('getfilter', info);
        }
    }
})

